package com.nariano.server.services.statistics.impl;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.nariano.server.domain.utils.PopulationTypeList;
import com.nariano.server.publisher.ClientPublisherRegister;
import com.nariano.server.services.cityservice.CityService;
import com.nariano.server.services.userservice.UserService;

public class StatsServiceImplFactory implements FactoryBean {
	
	@Autowired
	@Qualifier(value="clientPublisher")
    private ClientPublisherRegister clientPublisherRegister;
	
	@Autowired
    @Qualifier(value="userService")
    private UserService userService;
	
	@Autowired
    @Qualifier(value="cityService")
    private CityService cityService;
	
	@Autowired
    @Qualifier(value="populationDistributionContainer")
	private PopulationDistributionContainer populationDistributionContainer;
	
	private StatsServiceImpl instance = null;

	@Override
	public Object getObject() throws Exception {
		synchronized (clientPublisherRegister) {
			if (instance==null) {
				instance = new StatsServiceImpl(userService, cityService, populationDistributionContainer);
				clientPublisherRegister.add(instance);
			}
		}
		
		return instance;
	}

	@Override
	public Class getObjectType() {
		return StatsServiceImpl.class;
	}

	@Override
	public boolean isSingleton() {
		return true;
	}

}
